<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>HTML5模拟微信聊天界面</title>
  <style>
    /**重置标签默认样式*/
    * {
      margin: 0;
      padding: 0;
      list-style: none;
      font-family: '微软雅黑';
    }

    html {
      height: 100%;
    }

    body {
      height: 100%;
    }

    #container {
      width: 450px;
      height: 90%;
      background: #eee;
      margin: 0 auto 0;
      top: 5%;
      position: relative;
      box-shadow: 20px 20px 55px #777;
      border-radius: 0 0 5px 5px;
    }

    .header {
      background: #000;
      height: 40px;
      color: #fff;
      line-height: 40px;
      font-size: 16px;
      padding: 0 10px;
      border-radius: 5px 5px 0 0;
    }

    .footer {
      width: 430px;
      height: 50px;
      background: #666;
      position: absolute;
      bottom: 0;
      padding: 10px;
      border-radius: 0 0 5px 5px;
      font-size: 16px;
    }

    .footer input {
      width: 285px;
      height: 45px;
      outline: none;
      font-size: 16px;
      text-indent: 10px;
      position: absolute;
      border-radius: 6px;
      right: 80px;
    }

    .footer span {
      display: inline-block;
      width: 62px;
      height: 48px;
      background: #ccc;
      font-weight: 900;
      line-height: 45px;
      cursor: pointer;
      text-align: center;
      position: absolute;
      right: 10px;
      border-radius: 6px;
    }

    .footer span:hover {
      color: #fff;
      background: #999;
    }

    #user_face_icon {
      display: inline-block;
      background: red;
      width: 50px;
      height: 50px;
      border-radius: 30px;
      position: absolute;
      bottom: 10px;
      left: 14px;
      cursor: pointer;
      overflow: hidden;
    }

    img {
      width: 50px;
      height: 50px;
      border-radius: 10px;
    }

    .content {
      font-size: 20px;
      width: 435px;
      height: 662px;
      overflow: auto;
      padding: 5px;
      word-wrap: break-word;
      text-align: justify;
    }

    .content li {
      margin-top: 10px;
      padding-left: 10px;
      width: 412px;
      display: block;
      clear: both;
      overflow: hidden;
    }

    .content li img {
      float: left;
    }

    .content li span {
      background: #7cfc00;
      padding: 10px;
      border-radius: 10px;
      float: left;
      margin: 6px 10px 0 10px;
      max-width: 310px;
      border: 1px solid #ccc;
      box-shadow: 0 0 3px #ccc;
    }

    .content li img.imgleft {
      float: left;
    }

    .content li img.imgright {
      float: right;
    }

    .content li span.spanleft {
      float: left;
      background: #fff;
    }

    .content li span.spanright {
      float: right;
      background: #7cfc00;
    }
  </style>
</head>

<body>
  <div id="container">
    <!-- 顶部黑色头部 -->
    <div class="header">
      <span style="float: left;">知心姐姐</span>
      <span style="float: right;"></span>
    </div>
    <!-- 中间显示聊天内容 -->
    <ul class="content">
      <li v-for="msg in msgList">
        <template v-if="msg.tag">
          <img src="./img/right.png" class="imgright">
          <span class="spanright">{{msg.msg}}</span>
        </template>
        <template v-else>
          <img src="./img/left.png" class="imgleft">
          <span class="spanleft">{{msg.msg}}</span>
        </template>
      </li>
    </ul>
    <!-- 底部发送信息 -->
    <div class="footer">
      <div id="user_face_icon">
        <img src="./img/right.png" alt="" />
      </div>
      <input id="text" type="text" placeholder="说点什么吧..." v-model="msg" @keyup.enter="sendMsg" />
      <span id="btn" @click="sendMsg">发送</span>
    </div>
  </div>
</body>

</html>
<script src="./js/vue.js"></script>
<!-- 导包 -->
<script src="./js/axios.js"></script>
<!-- <script src="./js/jquery-1.12.4.min.js"></script> -->
<script>
  const app = new Vue({
    el: "#container",
    data: {
      msg: "",
      // 数组保存当前聊天的信息
      msgList: [{
          tag: true,
          msg: "你好啊，知心小姐姐"
        },
        {
          tag: false,
          msg: "你也好啊"
        }
      ]
    },
    updated(){
      //document.getElementsByClassName("content")[0].scrollTop = 99999999
    },
    methods: {
      // 给机器人发送消息，网络接口
      sendMsg() {
        // 第一步是将我自己的消息压进数组
        this.msgList.push({
          tag: true,
          msg: this.msg
        })
        // 如果要是传递参数的话，直接使用 event 对象即可
        // 第二步是发送数据请求，使用axios来发送数据请求
        axios.post('http://www.tuling123.com/openapi/api', {
            key: 'd7c82ebd8b304abeacc73b366e42b9ed',
            info: this.msg
          })
          .then((response) => {
            this.msgList.push({
              tag: false,
              msg: response.text
            })
            this.msg = ""
          })
          .catch((error) => {
            this.msgList.push({
              tag: false,
              msg: "我不在，有事打电话"
            })
            this.msg = ""
            console.log("错误是：" + error);
            /*
            setTimeout(() => {
              document.querySelector(".content").scrollTop = 99999999
            }, 100);
            */
            
          });
      }
    }
  })
</script>